package com.iwe3.protal.controller;


import com.iwe3.protal.common.CommonSelect;
import com.iwe3.protal.common.R;
import com.iwe3.protal.entity.BookTypeEntity;
import com.iwe3.protal.service.IBookTypeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "图书类别控制器")
@RequestMapping("/bookType")
@RestController // === @Controller + @ResponseBody
public class BookTypeController {

    private final IBookTypeService bookTypeServiceImpl;

    public BookTypeController(IBookTypeService bookTypeServiceImpl) {
        this.bookTypeServiceImpl = bookTypeServiceImpl;
    }

    // 完整路径：bookType/query -> BookTypeController # queryAllBookTypes()
    @Operation(summary = "查询所有图书类别", responses = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败"),
            @ApiResponse(responseCode = "404", description = "未找到")
    })
    @GetMapping("/query")
    public R queryAllBookTypes() {
        List<BookTypeEntity> all = bookTypeServiceImpl.findAll();
        /*使用stream 完成 数据转换*/
        List<CommonSelect> list = all.stream().map( e ->
                        new CommonSelect(e.getId() + "", e.getName())).
                collect(Collectors.toList());
        return R.ok(list);
    }
}
